{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.normal(0, 1, (8, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754,\n",
       "        -0.3774201 , -1.23842966, -0.52851946, -0.56424991,  0.14146128],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  1.31846868,  0.49385278,\n",
       "         1.00271502,  0.01556845,  0.32450184,  0.61851102, -0.4013447 ],\n",
       "       [-0.96500587,  1.46961001, -0.87826803, -0.45297124, -2.14897179,\n",
       "         0.68272824,  1.58313842,  0.38337549,  0.71032878,  1.08019164],\n",
       "       [-0.72965759,  0.8174701 , -0.69044323, -0.29653808, -1.39039668,\n",
       "         0.06887344,  1.19647011,  0.29994299,  0.26766034,  0.08409189],\n",
       "       [ 0.8812794 , -0.99043491,  0.07853688,  1.7188234 , -0.72888155,\n",
       "         0.86162266,  0.732064  , -0.66389352,  0.29972121,  0.65138616],\n",
       "       [ 2.78115686,  0.79727146, -0.13687709, -0.21730481, -1.23135718,\n",
       "         1.55976378,  1.15730037,  0.53211039,  1.26449498,  0.05871059],\n",
       "       [-1.8785593 ,  0.94328871,  0.05072624, -2.5209392 ,  0.86117011,\n",
       "         1.12231444,  2.2140556 ,  0.47748959,  0.65500665,  0.90449156],\n",
       "       [-0.97062524, -0.30895233,  0.93495808, -1.02327821, -0.46802391,\n",
       "        -0.30307614, -0.5838637 ,  0.25661242,  0.8567919 , -0.44501673]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c = stock_change[0:5, 0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  1.31846868,  0.49385278],\n",
       "       [-0.96500587,  1.46961001, -0.87826803, -0.45297124, -2.14897179],\n",
       "       [-0.72965759,  0.8174701 , -0.69044323, -0.29653808, -1.39039668],\n",
       "       [ 0.8812794 , -0.99043491,  0.07853688,  1.7188234 , -0.72888155]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False],\n",
       "       [False, False, False,  True, False],\n",
       "       [False,  True, False, False, False],\n",
       "       [False, False, False, False, False],\n",
       "       [False, False, False,  True, False]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c[stock_c > 1] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  2.        ,  0.49385278],\n",
       "       [-0.96500587,  2.        , -0.87826803, -0.45297124, -2.14897179],\n",
       "       [-0.72965759,  0.8174701 , -0.69044323, -0.29653808, -1.39039668],\n",
       "       [ 0.8812794 , -0.99043491,  0.07853688,  2.        , -0.72888155]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  2.        ,  0.49385278],\n",
       "       [-0.96500587,  2.        , -0.87826803, -0.45297124, -2.14897179],\n",
       "       [-0.72965759,  0.8174701 , -0.69044323, -0.29653808, -1.39039668],\n",
       "       [ 0.8812794 , -0.99043491,  0.07853688,  2.        , -0.72888155]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_d = stock_change[0:2, 0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  2.        ,  0.49385278]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(stock_d > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(stock_d> 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 1],\n",
       "       [0, 0, 1, 1, 1]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(stock_d > 0, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 1, 0],\n",
       "       [1, 1, 0, 0, 1]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_and(stock_d>-0.5, stock_d<0.5), 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 2],\n",
       "       [0, 0, 2, 2, 2]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_or(stock_d>0, stock_d<-1), 2, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.88350336, -0.68495187, -0.36713074, -0.0373903 ,  0.75573754,\n",
       "        -0.3774201 , -1.23842966, -0.52851946, -0.56424991,  0.14146128],\n",
       "       [-0.48247718, -0.20328427,  0.70120062,  2.        ,  0.49385278,\n",
       "         1.00271502,  0.01556845,  0.32450184,  0.61851102, -0.4013447 ],\n",
       "       [-0.96500587,  2.        , -0.87826803, -0.45297124, -2.14897179,\n",
       "         0.68272824,  1.58313842,  0.38337549,  0.71032878,  1.08019164],\n",
       "       [-0.72965759,  0.8174701 , -0.69044323, -0.29653808, -1.39039668,\n",
       "         0.06887344,  1.19647011,  0.29994299,  0.26766034,  0.08409189],\n",
       "       [ 0.8812794 , -0.99043491,  0.07853688,  2.        , -0.72888155,\n",
       "         0.86162266,  0.732064  , -0.66389352,  0.29972121,  0.65138616],\n",
       "       [ 2.78115686,  0.79727146, -0.13687709, -0.21730481, -1.23135718,\n",
       "         1.55976378,  1.15730037,  0.53211039,  1.26449498,  0.05871059],\n",
       "       [-1.8785593 ,  0.94328871,  0.05072624, -2.5209392 ,  0.86117011,\n",
       "         1.12231444,  2.2140556 ,  0.47748959,  0.65500665,  0.90449156],\n",
       "       [-0.97062524, -0.30895233,  0.93495808, -1.02327821, -0.46802391,\n",
       "        -0.30307614, -0.5838637 ,  0.25661242,  0.8567919 , -0.44501673]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.78115686, 2.        , 0.93495808, 2.        , 0.86117011,\n",
       "       1.55976378, 2.2140556 , 0.53211039, 1.26449498, 1.08019164])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.randint(low=1, high=10, size=(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 5],\n",
       "       [2, 6, 6],\n",
       "       [4, 6, 3]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 8, 6])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 6, 6])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1], dtype=int64)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.argmax(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4])\n",
    "arr + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 1. , 1.5, 2. ])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
